﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAOAuction;
using DTOAuction;
using System.Data.Linq;
using System.Data;

namespace BUSAuction
{
	/// <summary>
	/// author: hqlam
	/// </summary>
	public class BUSThongTinDangKy
	{
		public static bool themThongTinDangKy(int maSinhVien, int maLop)
		{
			return DAOThongTinDangKy.themThongTinDangKy(maSinhVien, maLop);
		}

		public static bool kiemTraThongTinDangKy(int maSinhVien, int maLop)
		{
			try
			{
				SINHVIEN sv = DAOSinhVien.layThongTinSinhVien(maSinhVien);
				LOP lop = DAOLop.layThongTinLop(maLop);
				// kiem tra khoa hoc cua sinh vien va lop
				if (sv.MAKHOAHOC != lop.HOCPHANMO.MAKHOAHOC)
					return false;

				// kiem tra khoa
				if (sv.MAKHOA != lop.HOCPHANMO.MONHOC.MAKHOA)
					return false;

				// kiem tra lop thuoc lich dang ky
				LICHDANGKY ldk = BUSLichDangKy.layLichDangKyHienTai();
				if (lop.HOCPHANMO.NAMHOC != ldk.NAMHOC || lop.HOCPHANMO.HOCKY != ldk.HOCKY)
					return false;

				// kiem tra sinh vien khong dang ky qua 2 lop 1 hoc phan
				List<LOP> lopDK = DAOThongTinDangKy.layDanhSachLopDaDangKy(maSinhVien);
				foreach (LOP l in lopDK)
					if (l.MAHPMO == lop.MAHPMO) return false;

				// kiem tra so tin chi toi da duoc phep dang ky


				return true;
			}
			catch
			{
				throw;
			}
		}

		public static DataTable layDanhSachLopDaDangKy(int maSinhVien)
		{
			DateTime now = DateTime.Now;
			int namHoc, hocKy;

			if (now.Month >= 1 && now.Month <= 6)
			{
				hocKy = 2;
				namHoc = now.Year - 1;
			}
			else
			{
				hocKy = 1;
				namHoc = now.Year;
			}

			return DAOThongTinDangKy.layDanhSachLopDaDangKy(maSinhVien, namHoc, hocKy);
		}

		public static DataTable layDanhSachLopCoTheDangKy(int maSinhVien)
		{
			DateTime now = DateTime.Now;
			int namHoc, hocKy;

			if (now.Month >= 1 && now.Month <= 6)
			{
				hocKy = 2;
				namHoc = now.Year - 1;
			}
			else
			{
				hocKy = 1;
				namHoc = now.Year;
			}

			return DAOThongTinDangKy.layDanhSachLopCoTheDangKy(maSinhVien, namHoc, hocKy);
		}

		public static bool huyDangKy(int maSinhVien, int maLop)
		{
			return DAOThongTinDangKy.huyDangKy(maSinhVien, maLop);
		}
	}
}
